﻿using AutoMapper;
using Core.Database.Model;
using Core.ValidateRules.Lib;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dto.Core.Database
{
    /// <summary>
    /// 数据库配置
    /// </summary>
    public class DatabaseConfigDto:BaseDto
    {
        #region 唯一标识

        /// <summary>
        /// 唯一标识
        /// </summary>
        private string? _guid;

        /// <summary>
        /// 唯一标识
        /// </summary>
        [JsonProperty("guid")]
        public string? Guid
        {
            get { return _guid; }
            set { SetProperty(ref _guid, value); }
        }

        #endregion 唯一标识 

        #region 连接组名称，为空默认赋值-guid，用于读写分离连接同组划分
        /// <summary>
        /// 连接组名称，为空默认赋值-guid，用于读写分离连接同组划分
        /// </summary>
        private string? _connectionGroup;

        /// <summary>
        /// 连接组名称，为空默认赋值-guid，用于读写分离连接同组划分
        /// </summary>
        [JsonProperty("connection_group")]
        public string? ConnectionGroup
        {
            get { return _connectionGroup; }
            set { SetProperty(ref _connectionGroup, value); }
        }
        #endregion 连接组名称，为空默认赋值-guid，用于读写分离连接同组划分

        #region 从库标识
        /// <summary>
        /// 从库标识
        /// </summary>
        private bool _slaveFlag = false;

        /// <summary>
        /// 从库标识
        /// </summary>
        [JsonProperty("slave_flag")]
        public bool SlaveFlag
        {
            get { return _slaveFlag; }
            set { SetProperty(ref _slaveFlag, value); }
        }
        #endregion 从库标识 

        #region 连接名称
        /// <summary>
        /// 连接名称
        /// </summary>
        private string? _connectionName;

        /// <summary>
        /// 连接名称
        /// </summary>
        [JsonProperty("connection_name")]
        [ValidateRulesRequired(ErrorMessage = "{0}不允许为空")]
        [ValidateRulesStringLength(50,MinimumLength =2,ErrorMessage = "{0}的文本长度为{2} - {1}")]
        [Display(Name = "连接名称")]
        public string? ConnectionName
        {
            get { return _connectionName; }
            set { SetProperty(ref _connectionName, value); }
        }

        #endregion 连接名称 

        /// <summary>
        /// 排序号
        /// </summary>
        [JsonProperty("sort_number")]
        public virtual int SortNumber { get; set; } = 1;

        #region 数据库名称

        /// <summary>
        /// 数据库名称
        /// </summary>
        private string? _databaseName;

        /// <summary>
        /// 数据库名称
        /// </summary>
        [JsonProperty("database_name")]
        [ValidateRulesRequired(ErrorMessage = "{0}不允许为空")]
        [ValidateRulesStringLength(128, MinimumLength = 1, ErrorMessage = "{0}的文本长度为{2} - {1}")]
        [Display(Name = "数据库名称")]
        public string? DatabaseName
        {
            get { return _databaseName; }
            set { SetProperty(ref _databaseName, value); }
        }

        #endregion 数据库名称 

        #region 数据库类型

        /// <summary>
        /// 数据库类型
        /// </summary>
        private DatabaseType? _databaseType;

        /// <summary>
        /// 数据库类型
        /// </summary>
        [JsonProperty("database_type")]
        [ValidateRulesRequired(ErrorMessage = "{0}不允许为空")]
        [Display(Name = "数据库类型")]
        public DatabaseType? DatabaseType
        {
            get { return _databaseType; }
            set { SetProperty(ref _databaseType, value); }
        }

        #endregion 数据库类型 

        #region 数据库地址

        /// <summary>
        /// 数据库地址
        /// </summary>
        private string? _address;

        /// <summary>
        /// 数据库地址
        /// </summary>
        [JsonProperty("address")]
        [ValidateRulesRequired(ErrorMessage = "{0}不允许为空")]
        [Display(Name = "数据库地址")]
        public string? Address
        {
            get { return _address; }
            set { SetProperty(ref _address, value); }
        }

        #endregion 数据库地址 

        #region 数据库端口

        /// <summary>
        /// 数据库端口
        /// </summary>
        private int _port;

        /// <summary>
        /// 数据库端口
        /// </summary>
        [JsonProperty("port")]
        [ValidateRulesRange(0, 65535, ErrorMessage = " {0} 取值范围：{1} - {2} ")]
        [Display(Name = "端口")]
        public int Port
        {
            get { return _port; }
            set { SetProperty(ref _port, value); }
        }

        #endregion 数据库端口 

        #region 连接池最大连接数

        /// <summary>
        /// 连接池最大连接数
        /// </summary>
        private uint _maxPoolSize = 5;

        /// <summary>
        /// 连接池最大连接数,默认值100
        /// </summary>
        [JsonProperty("max_pool_size")]
        [ValidateRulesRange(typeof(uint), "1", "65535", ErrorMessage = " {0} 取值范围：{2} - {1}")]
        [Display(Name = "连接池最大连接数")]
        public uint MaxPoolSize
        {
            get { return _maxPoolSize; }
            set { SetProperty(ref _maxPoolSize, value); }
        }

        #endregion 连接池最大连接数 

        #region 用户

        /// <summary>
        /// 用户
        /// </summary>
        private string? _userName;

        /// <summary>
        /// 用户
        /// </summary>
        [JsonProperty("user_name")]
        //[ValidateRulesRequired(ErrorMessage = "{0}不允许为空")]
        [Display(Name = "用户名")]
        public string? UserName
        {
            get { return _userName; }
            set { SetProperty(ref _userName, value); }
        }

        #endregion 用户 

        #region 密码

        /// <summary>
        /// 密码
        /// </summary>
        private string? _password;

        /// <summary>
        /// 密码
        /// </summary>
        [JsonProperty("password")]
        public string? Password
        {
            get { return _password; }
            set { SetProperty(ref _password, value); }
        }

        #endregion 密码 

        #region 超时时间（秒）

        /// <summary>
        /// 超时时间（秒）
        /// </summary>
        private uint _timeout = 60;

        /// <summary>
        /// 超时时间（秒）
        /// </summary>
        [JsonProperty("timeout")]
        public uint Timeout
        {
            get { return _timeout; }
            set { SetProperty(ref _timeout, value); }
        }

        #endregion 超时时间（秒） 

    }
}
